package com.ai.ai.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import java.io.IOException;

/**
 * 跨域过滤器
 * @author Scott Soong
 */
public class HttpCorsFilter implements Filter {
    /**
     * 对Http请求和响应增加Wrapper
     * @param request 请求对象
     * @param response 相应对象
     * @param chain 过滤器链
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ((HttpServletResponse)response).setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
        ((HttpServletResponse)response).setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "*");
        ((HttpServletResponse)response).setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
        ((HttpServletResponse)response).setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
        chain.doFilter(request, response);
    }
}
